/*
 *  Copyright (C) 2010 Taylor Leese (tleese22@gmail.com)
 *
 *  This file is part of jappstart.
 *
 *  jappstart is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  jappstart is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with jappstart.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.dk.poitalk.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;

import org.dk.poitalk.service.sello.QRCodeService;
import org.dk.poitalk.service.sello.impl.QRCodeServiceImpl.FormatImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * The registration controller.
 */
/**
 * @author romemigu
 * 
 */
@Controller
@RequestMapping("/qrcode")
public class QRCodeController {

	private static final Logger log = Logger.getLogger(QRCodeController.class
			.getName());
	
	private QRCodeService qrCodeService;

	
	public final QRCodeService getQrCodeService() {
		return qrCodeService;
	}

	@Autowired
	public final void setQrCodeService(QRCodeService qrCodeService) {
		this.qrCodeService = qrCodeService;
	}

	@RequestMapping(value = "/generateQRCode", method = RequestMethod.GET)
	public ResponseEntity<byte[]> generateQRCode(
			final HttpServletRequest request) throws IOException {

		String key = request.getParameter("key"); 
		FormatImage formatImage = FormatImage.png;
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		qrCodeService.writeQRCodeToStream(key, 200, 200, byteArrayOutputStream,
				formatImage, 9);

		log.log(Level.INFO, "Se ha generado el array de este tamaño "
				+ byteArrayOutputStream.size());

		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.set("Content-Disposition", "attachment; filename="
				+ key + "." + formatImage.toString());

		switch (formatImage) {
		case png:
			responseHeaders.setContentType(MediaType.IMAGE_PNG);
			break;
		}

		responseHeaders.setContentLength(byteArrayOutputStream.size());

		log.log(Level.INFO, "El ToString de responseHeaders es "
				+ responseHeaders.toString());

		return new ResponseEntity<byte[]>(
				(byte[]) byteArrayOutputStream.toByteArray(), responseHeaders,
				HttpStatus.CREATED);
	}
	
}
